package com.example.boos.ui.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.example.boos.R;
import com.example.boos.pojo.FoodType;

import java.util.List;

/**
 * Created by Vin on 08/12/2016.
 */

// 菜品类型 数据填充
public class LeftAdapter extends BaseAdapter{

    private Context mContext;

    private List<FoodType> foodTypes;

    private LayoutInflater mInflater;

    private int mSelectedPosition = 0;

    public LeftAdapter( Context mContext,  List<FoodType> mData) {
        this.mContext = mContext;
        this.foodTypes = mData;
        mInflater = LayoutInflater.from(mContext);
    }


    // 填充数据
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder holder;
        if (null == view) {
            view = mInflater.inflate(R.layout.left_item,null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }

        // 填充数据
        FoodType model = foodTypes.get(i);
        holder.name.setText(model.getFoodTypeName());
        // 判断当前的position 是否为选中的 position ,如果是
        view.setBackgroundResource(mSelectedPosition == i ? R.drawable.left_item_selected : R.drawable.left_item_default);

        return view;
    }

    // 选中新的 菜品类型？
    public void updateSelected(int position){
        this.mSelectedPosition = position;
        notifyDataSetChanged();
    }


    public void update(int position,FoodType model){
        //  更新菜品类型链表中的数据
        foodTypes.set(position,model);
        notifyDataSetChanged(); // ui线程调用 getView()去更新数据
    }

    public void updateAll(List<FoodType>data){
        this.foodTypes = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return foodTypes.size();
    }

    @Override
    public Object getItem(int i) {
        return foodTypes.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    class ViewHolder {

        TextView name;

        public ViewHolder(View view) {
            name = (TextView) view.findViewById(R.id.name);
        }
    }
}
